package com.nsgf.library.utils;


import android.content.Context;
import android.content.SharedPreferences;
import android.text.format.DateUtils;

import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

public class SignInManager {
    private static final String PREF_NAME = "sign_in_pref";
    private static final String KEY_LAST_SIGN_IN_DATE = "last_sign_in_date";
    private static final String KEY_CURRENT_WEEK = "current_week";
    private static final String KEY_TOTAL_DAYS = "total_sign_in_days";
    private static final String KEY_CURRENT_STREAK = "current_streak";
    private static final String KEY_SIGNED_DAYS = "signed_days";

    private SharedPreferences preferences;
    private int currentWeek = 0;  // 当前显示的周数
    private int totalSignInDays = 0;  // 总签到天数
    private int currentStreak = 0;  // 当前连续签到天数
    private boolean[] signedDays = new boolean[7];  // 当前周的签到状态

    public SignInManager(Context context) {
        preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        loadSignInData();
    }

    private void loadSignInData() {
        currentWeek = preferences.getInt(KEY_CURRENT_WEEK, 0);
        totalSignInDays = preferences.getInt(KEY_TOTAL_DAYS, 0);
        currentStreak = preferences.getInt(KEY_CURRENT_STREAK, 0);

        // 加载当前周的签到状态
        String signedDaysStr = preferences.getString(KEY_SIGNED_DAYS, "0000000");
        for (int i = 0; i < 7; i++) {
            signedDays[i] = signedDaysStr.charAt(i) == '1';
        }
    }

    private void saveSignInData() {
        preferences.edit()
                .putInt(KEY_CURRENT_WEEK, currentWeek)
                .putInt(KEY_TOTAL_DAYS, totalSignInDays)
                .putInt(KEY_CURRENT_STREAK, currentStreak)
                .putString(KEY_SIGNED_DAYS, getSignedDaysString())
                .apply();
    }

    private String getSignedDaysString() {
        StringBuilder sb = new StringBuilder();
        for (boolean signed : signedDays) {
            sb.append(signed ? "1" : "0");
        }
        return sb.toString();
    }

    public boolean[] getWeekSignStatus() {
        // 检查是否需要进入下一周
        if (isCurrentWeekComplete()) {
            currentWeek++;
            resetWeekStatus();
            saveSignInData();
        }
        return signedDays;
    }

    private boolean isCurrentWeekComplete() {
        // 检查当前周是否全部签到完成
        for (boolean signed : signedDays) {
            if (!signed) {
                return false;
            }
        }
        return true;
    }

    private void resetWeekStatus() {
        // 重置当前周的签到状态
        for (int i = 0; i < 7; i++) {
            signedDays[i] = false;
        }
    }

    public boolean signIn(int dayIndex) {
        if (dayIndex < 0 || dayIndex >= 7 || signedDays[dayIndex]) {
            return false;
        }

        // 检查是否是连续签到
        boolean isConsecutive = dayIndex == 0 || (dayIndex > 0 && signedDays[dayIndex - 1]);

        signedDays[dayIndex] = true;
        totalSignInDays++;

        if (isConsecutive) {
            currentStreak++;
        } else {
            currentStreak = 1;
        }

        saveSignInData();
        return true;
    }

    public int getCurrentStreak() {
        return currentStreak;
    }

    public int getTotalSignInDays() {
        return totalSignInDays;
    }

    public int getCurrentWeek() {
        return currentWeek;
    }

    public int getRewardForDay(int dayIndex) {
        // 根据当前周数和天数计算奖励
        int day = currentWeek * 7 + dayIndex + 1;
        switch (day % 7) {
            case 0: return 100;  // 第7天
            case 1: return 10;   // 第1天
            case 2: return 20;   // 第2天
            case 3: return 30;   // 第3天
            case 4: return 40;   // 第4天
            case 5: return 50;   // 第5天
            case 6: return 60;   // 第6天
            default: return 0;
        }
    }
}